
package grisar.gris.logic;

import grisar.gris.helpers.DataHolder;
import grisar.gris.helpers.ResourceAllocator;
import grisar.gris.struct.I_Drawable;
import grisar.gris.struct.I_GameObject;
import grisar.gris.struct.MPoint;
import grisar.gris.struct.enemy.Enemy;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import android.R;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;

public class Spawner implements I_GameObject, I_Drawable {
	
	public static final String ENEMY_DEC = "enemy";
	public static final String EVENT_DEC = "event";
	
	private Map<Integer,EnemyDesc> enemyMap;
	private List<SpawnLogic> events;
	private List<SpawnLogic> activeEvents;
	private int ticker;
	private Point pos;
	private Paint paint;
	private Bitmap bitmap;
	private LinkedList<MPoint> path;
	
	public Spawner(LinkedList<MPoint> path){
		
		this.path = path;
		bitmap = ResourceAllocator.getDrawable(grisar.gris.R.drawable.tower3a, true);
		paint = new Paint();
		ticker = 0;
		enemyMap = new HashMap<Integer,EnemyDesc>();
		events = new LinkedList<SpawnLogic>();
		activeEvents = new LinkedList<SpawnLogic>();
		pos = new Point(0,0);
		
	}
	
	public void loadScript(String script){
		
		enemyMap = new HashMap<Integer,EnemyDesc>();
		events = new LinkedList<SpawnLogic>();
		
		String[] data = script.split("\n");
		int i=0;
		while(i<data.length){
			if(data[i].equals(ENEMY_DEC)){
				i++;
				EnemyDesc eDesc = new EnemyDesc();
				int id = Integer.valueOf(data[i]);
				i++;
				String[] hp = data[i].split("/");
				i++;
				eDesc.setHPRange(Integer.valueOf(hp[0]), Integer.valueOf(hp[1]));
				String[] speed = data[i].split("/");
				i++;
				eDesc.setSpeedRange(Integer.valueOf(speed[0]), Integer.valueOf(speed[1]));
				enemyMap.put(id, eDesc);
				System.out.println("Add char");
			}
			else if(data[i].equals(EVENT_DEC)){
				i++;
				int id = Integer.valueOf(data[i]);
				i++;
				String[] time = data[i].split("/");
				i++;
				SpawnLogic sEvent = new SpawnLogic(Integer.valueOf(time[0]),Integer.valueOf(time[1]),Integer.valueOf(time[2]),id);
				events.add(sEvent);
				System.out.println("Add event");
			}
		}
		SpawnLogic sEvent = new SpawnLogic(0,300,60,2);
		activeEvents.add(sEvent);
	}
	
	//TODO Make sexier, love longtime! thx
	public void update(){
		
		ticker++;
		
		List<SpawnLogic> temp = new LinkedList<SpawnLogic>(); 
		for(SpawnLogic s : events){
			if(ticker > s.getStartTime()){
				temp.add(s);
			}
		}
		for(SpawnLogic s : temp){
			events.remove(s);
			activeEvents.add(s);
		}
		temp.clear();
		
		for(SpawnLogic s : activeEvents){
			s.update(ticker);
			
			//TODO spawn enemy
			if(s.isSpawning()){
				Enemy enemy = new Enemy(path, ResourceAllocator.getDrawable(grisar.gris.R.drawable.tower2a, true));
				enemy.setPosition(new Point(pos.x,pos.y));
				DataHolder.getEnemies().add(enemy);			
			}
			
			if(ticker == s.getEndTime()){
				temp.add(s);
			}
		}
		
		for(SpawnLogic s : temp){
			activeEvents.remove(s);
		}
	}
	
	public static String scriptEnemy(int id, int minHP, int maxHP, int minSpeed, int maxSpeed){
		
		return ENEMY_DEC + "\n" + id + "\n" +  minHP + "/" + maxHP + "\n" + minSpeed + "/" + maxSpeed + "\n";
	}
	
	public static String scriptEvent(int id, int startTime, int endTime, int spawnInterval){
		
		return EVENT_DEC + "\n" + id + "\n" +  startTime + "/" + endTime + "/" + spawnInterval + "\n";
	}

	public Point getPosition() {
		
		return pos;
	}

	public void setPosition(Point pos) {
		
		this.pos = pos;
	}

	@Override
	public void draw(Canvas canvas) {

		canvas.drawBitmap(bitmap, pos.x, pos.y, paint);
		canvas.drawText("Here i am " + activeEvents.size(), pos.x, pos.y, paint);
	}
}
